package com.ricky.onlineShop.controller.trading;

import com.ricky.common.config.RickyConfig;
import com.ricky.common.core.domain.AjaxResult;
import com.ricky.common.utils.file.FileUploadUtils;
import com.ricky.common.utils.file.FileUtils;
import com.ricky.common.utils.uuid.UUID;
import com.ricky.framework.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author staticlan
 * @Date 2024/6/21 16:26
 * @Description:
 */
@RestController
@RequestMapping("/onlineShop/shopPicture")
public class ShopPictureController {

    @Value("${ricky.uploadPath}")
    private String uploadPath; // 从配置文件读取上传路径

    @Autowired
    private ServerConfig serverConfig;

    private static final String FILE_DELIMETER = ",";
    @PostMapping("/uploadShopPictures")
    public AjaxResult uploadShopPictures(MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = RickyConfig.getUploadPath();
            String uid = UUID.randomUUID().toString();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(uploadPath + "/" + uid, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}